package org.wlgzs.office_exam_student.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.wlgzs.office_exam_student.dao.BaseDao;
import org.wlgzs.office_exam_student.entity.OperateQuestion;

/**
*@author song(mejeesong@qq.com)
*2018年1月30日
*
*/
public class OperateQuestionService extends BaseDao<OperateQuestion>{

	public List<OperateQuestion> operateQuestions(int schoolId, int questionNumber, String type, String grade) {

		return exec(qr -> {
			try {
				return qr.query(
						"SELECT * FROM tb_operate_question where type = ? and grade = ? ORDER BY RAND() LIMIT ?",
						BEAN_LIST_HANDLER, type,grade, questionNumber);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return new ArrayList<>();
		});
	}
	public List<OperateQuestion> queryAll(){
		List<OperateQuestion> qs = exec(qr -> {
			try {
				return qr.query("select * from tb_operate_question",BaseDao.BEAN_LIST_HANDLER);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
		});
		return qs;
	}
	public void insert(OperateQuestion oq) {
		String sql = "insert into tb_operate_question (grade,type,title,answer,analysis,source_url,score)values(?,?,?,?,?,?,?)";
		exec( qr -> {
			try {
				return qr.update(sql, oq.getGrade(),oq.getType(),oq.getTitle(),oq.getAnswer(),oq.getAnalysis(),oq.getSourceUrl(),oq.getScore());
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
		});
	}


    public OperateQuestion operateQuestion(int questionId) {

        OperateQuestion operateQuestion = exec(qr -> {
            try {
                return qr.query(
                        "SELECT * FROM tb_operate_question where id = ?",
                        BEAN_HANDLER, questionId);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        });
        return operateQuestion;
    }
    public OperateQuestion operateQuestion(String sourceUrl) {

        OperateQuestion operateQuestion = exec(qr -> {
            try {
                return qr.query(
                        "SELECT * FROM tb_operate_question where id = ?",
                        BEAN_HANDLER, sourceUrl);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        });
        return operateQuestion;
    }
    
    
    public void update(OperateQuestion oq) {
    	String sql = "update tb_operate_question set grade = ?,type = ?,title = ?,answer = ?,analysis = ?,source_url = ?,score = ? where id = ?";
		exec( qr -> {
			try {
				return qr.update(sql, oq.getGrade(),oq.getType(),oq.getTitle(),oq.getAnswer(),oq.getAnalysis(),oq.getSourceUrl(),oq.getScore(),oq.getId());
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return null;
		});
    }

}
